﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour
{
    public Collider2D crouchCollider;
    public float speed;
    public float jumpforce;
    public AudioSource jumpSource;
    public AudioSource hurtSource;
    public AudioSource collectionSource;
    public AudioSource deadLine;
    public CircleCollider2D coll;
    public LayerMask ground;
    public Text cherryText;
    public Text gemText;
    public Transform ceilingPos;
    public Transform jumpPos;

    private Animator anim;
    private Rigidbody2D playerRb;
    private int cherryScore = 0;
    private int gemScore = 0;
    private bool isHurt = false;
    private bool isGround;
    private int jumpCnt;

    // Start is called before the first frame update
    private void Start()
    {
        anim = GetComponent<Animator>();
        playerRb = GetComponent<Rigidbody2D>();
    }
    // Update is called once per frame
    private void Update()
    {
        AirHike();
    }
    void FixedUpdate()
    {
        if (!isHurt)
        {
            PlayerMove();
            Crouch();
        }
        
        SwitchAnim();
        isGround = Physics2D.OverlapCircle(jumpPos.position, 0.2f, ground);

    }
    void PlayerMove()
    {
        float horizontalMove = Input.GetAxis("Horizontal");
        float faceDirction = Input.GetAxisRaw("Horizontal");
        //Player Move
        if (horizontalMove != 0)
        {
            playerRb.velocity = new Vector2(horizontalMove * speed * Time.fixedDeltaTime, playerRb.velocity.y);
            anim.SetFloat("running", Mathf.Abs(faceDirction)); 
        }
        if (faceDirction != 0)
        {
            transform.localScale = new Vector3(faceDirction, transform.localScale.y, transform.localScale.z);
        }
        //跳起动画
        /*if (Input.GetButtonDown("Jump") && coll.IsTouchingLayers(ground))
        {
            jumpSource.Play();
            playerRb.velocity = new Vector2(playerRb.velocity.x, jumpforce * Time.deltaTime);
            anim.SetBool("jumping", true);
        }   */
    }

    void AirHike()
    {
        if (isGround)
        {
            jumpCnt = 1;
        }
        if(Input.GetButtonDown("Jump") && jumpCnt > 0)
        {
            jumpCnt--;
            anim.SetBool("jumping", true);
            anim.SetBool("falling", false);
            playerRb.velocity = Vector2.up * jumpforce;
        }
    }
    void SwitchAnim()
    {
        //下落动画
        anim.SetBool("idle",false);
        if (anim.GetBool("jumping"))
        {
            if(playerRb.velocity.y < 0)
            {
                anim.SetBool("falling", true);
                anim.SetBool("jumping", false);         
            }
        }
        //受伤时
        else if (isHurt)
        {
            if(Mathf.Abs(playerRb.velocity.x) < 0.1f)
            {
                isHurt = false;
                anim.SetBool("hurting",false);
                anim.SetFloat("running", 0);
            }
        }
        //下落碰到地面
        else if (coll.IsTouchingLayers(ground))
        {
            anim.SetBool("falling", false);
            anim.SetBool("idle", true);
        }
    }

    //触发检测器
    private void OnTriggerEnter2D(Collider2D collision)
    {
        //收集计分
        if (collision.tag == "Collection")
        {
            collectionSource.Play();
            if (collision.transform.parent.name == "Cherry")
            {
                cherryScore++;
                Destroy(collision.gameObject);
                cherryText.text = cherryScore.ToString();
            }
            else
            {
                gemScore++;
                Destroy(collision.gameObject);
                gemText.text = gemScore.ToString();
            }
        }
        if(collision.tag == "DeadLine")
        {
            deadLine.Play();
            Invoke("Restart", 2f);
        }
    }

    //碰撞检测器
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.tag == "Enemy")
        {
            Enemy enemy = collision.gameObject.GetComponent<Enemy>();
            //销毁敌人
            if (anim.GetBool("falling"))
            {
                enemy.Death();
                playerRb.velocity = new Vector2(playerRb.velocity.x, 800 * Time.deltaTime);
                anim.SetBool("jumping", true);
            }
            //碰撞敌人
            else if (playerRb.transform.position.x < collision.transform.position.x)
            {
                hurtSource.Play();
                playerRb.velocity = new Vector2(-400 * Time.deltaTime, playerRb.velocity.y);
                isHurt = true;
                anim.SetBool("hurting", true);
            }
            else if (playerRb.transform.position.x > collision.transform.position.x)
            {
                hurtSource.Play();
                playerRb.velocity = new Vector2(400 * Time.deltaTime, playerRb.velocity.y);
                isHurt = true;
                anim.SetBool("hurting", true);
            }
        }
    }
    //蹲下
    void Crouch()
    {
        if(!Physics2D.OverlapCircle(ceilingPos.position,0.2f,ground))
        {
            if (Input.GetButton("Crouch"))
            {
                anim.SetBool("isCrouch", true);
                crouchCollider.enabled = false;
            }
            else
            {
                anim.SetBool("isCrouch", false);
                crouchCollider.enabled = true;
            }
        }
    }
    void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

}
